package com.feihao.zhou.ThreadSave;

import java.util.concurrent.atomic.AtomicInteger;

public class Ticket implements Runnable {

//    private /*volatile*/ int ticket = 100;
    private AtomicInteger ato = new AtomicInteger(100);

    @Override
    public void run() {

        while (true) {

            int i = ato.decrementAndGet();
//            synchronized (this) {
                if (i > 0) { //票还有，则继续卖
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName() + "正在卖票，还有" + i + "张票");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }


           /* for (int i1 = 0; i1 < 100; i1++) {
                int count = ato.decrementAndGet();
                System.out.println(count);
            }*/
//            }
        }

    }
}


/*//无锁对象，有线程安全问题，重复票！！！
            if (ticket > 0) { //票还有，则继续卖
                try {
                    Thread.sleep(100);
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票，还有" + ticket + "张票");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                break;
            }
        }*/